package com.lz.app.demo.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.lz.app.demo.annotations.TokenRequired;
import com.lz.app.demo.configuration.RongCloudConfig;
import com.lz.app.demo.dao.TblLanzuoMeetingDao;
import com.lz.app.demo.dao.TblLanzuoMeetingMemberDao;
import com.lz.app.demo.domain.TblLanzuoMeeting;
import com.lz.app.demo.domain.TblLanzuoMeetingMember;
import com.lz.app.demo.dto.request.MeetingMemberRequestDto;
import com.lz.app.demo.dto.request.MeetingRequestDto;
import com.lz.app.demo.dto.request.MeetingRequestUserDto;
import com.lz.app.demo.dto.response.MeetingMemberResponseDto;
import com.lz.app.demo.dto.response.MeetingResponseDto;
import com.lz.app.demo.util.mapper.BeanMapperTemplate;
import io.rong.RongCloud;
import io.rong.messages.ImgTextMessage;
import io.rong.methods.message._private.Private;
import io.rong.methods.message.system.MsgSystem;
import io.rong.models.message.SystemMessage;
import io.rong.models.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: MeetingController
 * @ProjectName app-service
 * @Author huchao
 * @Date 2018/9/4上午10:52
 */
@RestController
@RequestMapping(value = "/meeting")
@Slf4j
@TokenRequired
@Api(description = "会议室")
public class MeetingController {

	@Autowired
	private TblLanzuoMeetingDao tblLanzuoMeetingDao;

	@Autowired
	private TblLanzuoMeetingMemberDao tblLanzuoMeetingMemberDao;

	@Autowired
	private BeanMapperTemplate beanMapperTemplate;

	@Autowired
	private RongCloud rongCloud;


	@RequestMapping(value = "/created", method = RequestMethod.GET)
	@ApiOperation(value = "我创建的会议列表")
	public List<MeetingResponseDto> listByOwnerId(HttpServletRequest httpServletRequest) {
		Integer userId = (Integer) httpServletRequest.getAttribute("userId");
		List<TblLanzuoMeeting> meetingList = tblLanzuoMeetingDao.listByOwnId(userId);
		return beanMapperTemplate.mapToList(meetingList, MeetingResponseDto.class);
	}

	@RequestMapping(value = "/attend", method = RequestMethod.GET)
	@ApiOperation(value = "我参与的会议列表")
	public List<MeetingResponseDto> listByUserId(HttpServletRequest httpServletRequest) {
		Integer userId = (Integer) httpServletRequest.getAttribute("userId");
		List<TblLanzuoMeeting> meetingList = tblLanzuoMeetingDao.listByUserId(userId);
		return beanMapperTemplate.mapToList(meetingList, MeetingResponseDto.class);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ApiOperation(value = "会议详情")
	public MeetingResponseDto findById(@PathVariable(value = "id") Integer id) throws Exception {
		TblLanzuoMeeting tblLanzuoMeeting = tblLanzuoMeetingDao.findById(id);
		if (tblLanzuoMeeting != null) {
			MeetingResponseDto meetingResponseDto = beanMapperTemplate.map(tblLanzuoMeeting, MeetingResponseDto.class);
			List<TblLanzuoMeetingMember> tblLanzuoMeetingMemberList = tblLanzuoMeetingMemberDao.findTblLanzuoMeetingMembersByMeetingId(id);
			meetingResponseDto.setMemberList(beanMapperTemplate.mapToList(tblLanzuoMeetingMemberList, MeetingMemberResponseDto.class));
			return meetingResponseDto;
		} else {
			throw new Exception("无效的会议");
		}
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	@Transactional(rollbackFor = Exception.class)
	@ApiOperation(value = "创建会议")
	public MeetingResponseDto save(@RequestBody MeetingRequestDto meetingRequestDto, HttpServletRequest httpServletRequest) {
		Integer userId = (Integer) httpServletRequest.getAttribute("userId");
		List<MeetingRequestUserDto> users = meetingRequestDto.getUsers();
		TblLanzuoMeeting tblLanzuoMeeting = beanMapperTemplate.map(meetingRequestDto, TblLanzuoMeeting.class);
		tblLanzuoMeeting.setUserId(userId);
		tblLanzuoMeeting = tblLanzuoMeetingDao.save(tblLanzuoMeeting);
		List<TblLanzuoMeetingMember> meetingMemberList = new ArrayList<>();
		for (MeetingRequestUserDto item : users) {
			TblLanzuoMeetingMember tblLanzuoMeetingMember = new TblLanzuoMeetingMember();
			tblLanzuoMeetingMember.setUserId(item.getId());
			tblLanzuoMeetingMember.setUsername(item.getName());
			tblLanzuoMeetingMember.setMeetingId(tblLanzuoMeeting.getId());
			tblLanzuoMeetingMember.setStatus(2);
			tblLanzuoMeetingMember = tblLanzuoMeetingMemberDao.save(tblLanzuoMeetingMember);
			meetingMemberList.add(tblLanzuoMeetingMember);
		}
		MeetingResponseDto meetingResponseDto = beanMapperTemplate.map(tblLanzuoMeeting, MeetingResponseDto.class);
		meetingResponseDto.setMemberList(beanMapperTemplate.mapToList(meetingMemberList, MeetingMemberResponseDto.class));
		return meetingResponseDto;
	}

	@RequestMapping(value = "/member/{memberId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改会议的参与状态")
	public void updateMeetingMember(@PathVariable(value = "memberId") Integer memberId, @RequestBody MeetingMemberRequestDto meetingMemberRequestDto) {
		TblLanzuoMeetingMember tblLanzuoMeetingMember = tblLanzuoMeetingMemberDao.findTblLanzuoMeetingMemberById(memberId);
		tblLanzuoMeetingMember.setStatus(meetingMemberRequestDto.getStatus());
		tblLanzuoMeetingMemberDao.save(tblLanzuoMeetingMember);
	}

	private void sendMeetingMsg() throws Exception {
		String content = null;
		String extra = null;
		String title = null;
		String url = null;
		ImgTextMessage itm = new ImgTextMessage(content, extra, title, null, url);

		MsgSystem system = rongCloud.message.system;
		Private Private = rongCloud.message.msgPrivate;
		String[] targetIds = {"uidmichael1"};
		SystemMessage systemMessage = new SystemMessage()
				.setSenderId("uidmichael")
				.setTargetId(targetIds)
				.setObjectName(itm.getType())
				.setContent(itm)
				.setPushContent("this is a push!")
				.setPushData("{'pushData':'hello'}")
				.setIsPersisted(0)
				.setIsCounted(0)
				.setContentAvailable(0);

		ResponseResult result = system.send(systemMessage);
	}

}
